package com.txunda.zhuohired.fgt.user.my;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Switch;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshBase;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshListView;
import com.ants.theantsgo.view.pulltorefresh.PullToRefreshScrollView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.my.CareerProgressAdapter;
import com.txunda.zhuohired.aty.user.my.CareerProgressDetailsAty;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.user.my.CareerProgressBean;
import com.txunda.zhuohired.http.pst.InfoPst;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/9 10:32
 * 功能描述：求职进展单个fragment
 * 联系方式：jiahuanchen@126.com
 */

public class CareerProgressFgt extends BaseFgt {

    /**
     * listView
     */
    @ViewInject(R.id.lv_career_progress)
    private PullToRefreshListView lv_career_progress;
    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;
    private int status_type = 1;
    private InfoPst infoPst;
    private String token = "";
    private int page = 1;
    private String type ;
    private int id;
    private List<CareerProgressBean.DataBean> careerList;
    private CareerProgressAdapter careerProgressAdapter;

    @Override
    protected void immersionInit() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_career_progress;
    }

    @Override
    protected void initialized() {
        token = application.getUserInfo().get("data");
        infoPst = new InfoPst(this);
    }

    @Override
    protected void requestData() {
    }

    private void initHttp(){
        infoPst.jobMarch(token,String.valueOf(status_type),String.valueOf(page));
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        lv_career_progress.onRefreshComplete();
        if(requestUrl.contains("jobMarch")){
            L.e("=====jobMarch====",jsonStr);
            CareerProgressBean careerProgressBean = GsonUtil.GsonToBean(jsonStr,CareerProgressBean.class);
            careerList = careerProgressBean.getData();
            if(page == 1){
                if(careerList.size()==0){
                    noData(2);
                }else{
                    noData(1);
                    initAdapter();
                }
            }else{
                if(careerProgressBean.getData().size()==0){
                    showToast("已经最后一页了，没有数据了");
                }else{
                    List myEventList = careerProgressBean.getData();
                    careerList.addAll(myEventList);
                    careerProgressAdapter.notifyDataSetChanged();
                }
            }
            initAdapter();
        }
    }

        /**
     * 空数据
     *
     * @param type
     */
    private void noData(int type){
        if(type==1){
            lv_career_progress.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        }else{
            lv_career_progress.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 每次fragment可见时
     * <p/>
     * 请求接口
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    initHttp();
                }
            }, 100);
        } else {
            // 不可见时不执行操作
        }
    }



    private void initAdapter() {
        careerProgressAdapter = new CareerProgressAdapter(getActivity(),careerList);
        lv_career_progress.setAdapter(careerProgressAdapter);
        lv_career_progress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                String id = careerList.get(i-1).getId();
                String status  = careerList.get(i-1).getStatus();
                bundle.putString("status",status);
                bundle.putString("status_type",String.valueOf(status_type));
                bundle.putString("id",id);
                startActivity(CareerProgressDetailsAty.class,bundle);

            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lv_career_progress.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initHttp();//请求接口
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = page + 1;
                initHttp();//请求接口
            }
        });

    }

    public static CareerProgressFgt getFgt(int type) {
        CareerProgressFgt careerProgressFgt = new CareerProgressFgt();
        careerProgressFgt.status_type = type;
        return careerProgressFgt;
    }
}
